package com.snail.jj.androiddemos.components;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.snail.jj.androiddemos.R;
import com.snail.jj.androiddemos.bases.BaseActivity;

/**
 * Created by snail on 2017/5/12.
 */

public class BroadcastReceiverActivity extends BaseActivity {

    MyReceiver receiver = new MyReceiver();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_boardcastreceiver);
        initView();
    }

    public void initView() {
        setTitleBarTitle("BroadcasrReceiver");
        setRightViewVisiable(View.VISIBLE);
    }

    @Override
    public void rightViewAction() {
        super.rightViewAction();
        //普通广播
//        Intent intent = new Intent("android.intent.action.MY_BROADCAST");
//        intent.putExtra("msg","hello receiver");
//        sendBroadcast(intent);

        //有序广播
        Intent intent = new Intent("android.intent.action.MY_BROADCAST");
        intent.putExtra("msg","hello receiver");
        //sendOrderedBroadcast 发送有序广播时，需要一个权限参数。需要在AndroidMainfest.xml 加入权限
        sendOrderedBroadcast(intent,"scott.permission.MY_BROADCAST_PERMISSION");
    }

    public void registerReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction("android:intent.action.MY_BROADCAST");
        registerReceiver(receiver,filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Activity销毁后，需要解除注册
//        unregisterReceiver(receiver);
    }
}
